#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
enum
{
    LEFT,
    RIGHT,
    ROOT
};
struct Node
{
    int flag;
    TreeNode *ptr;
    Node(int flag, TreeNode *ptr) : flag(flag), ptr(ptr){};
};
queue<Node> que;
class Solution
{
public:
    int sum = 0;
    bool isleaf(TreeNode *nd) { return nd != NULL && nd->left == NULL && nd->right == NULL; };
    int sumOfLeftLeaves(TreeNode *root)
    {
        que.emplace(ROOT, root);
        while (que.empty() == false)
        {
            Node nd = que.front();
            que.pop();
            if (nd.ptr == NULL)
                continue;
            if (nd.flag == LEFT && isleaf(nd.ptr))
                sum += nd.ptr->val;
            que.emplace(LEFT, nd.ptr->left);
            que.emplace(RIGHT, nd.ptr->right);
        }
        return sum;
    }
};